package com.yhp.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.system.comm.util.BaseUtils;
import com.system.common.pojo.BisShop;
import com.system.common.pojo.BisShopPost;
import com.system.common.pojo.BisShopUser;
import com.yhp.service.BBisShopPostService;
import com.yhp.service.BBisShopService;
import com.yhp.service.BBisShopUserService;

@Controller
@RequestMapping("bsu")
public class BBisShopUserController {
	@Autowired
	private BBisShopUserService userService;
	@Autowired
	private BBisShopService shopService;
	@Autowired
	private BBisShopPostService postService;
	
	/**
	 * 员工信息>>>>>>综合查询
	 * @param model
	 * @return
	 */
	@RequestMapping("user")
	public String user(Model model,HttpServletRequest request){
		Map<String, Object> map = initMap(request);
		List<BisShopUser> userList = userService.selectByExample(map);
		for (BisShopUser bisShopUser : userList) {
			String password=BaseUtils.decrypt(bisShopUser.getPassword(), BaseUtils.PASSWORD_KEY);
			bisShopUser.setPassword(password);
		}
		model.addAttribute("userList", userList);
		
		List<BisShop> shopList = shopService.selectByExample();
		model.addAttribute("shopList", shopList);
		
		List<BisShopPost> postList = postService.selectByExample();
		model.addAttribute("postList", postList);
		return "bis_shop/user";
	}
	
	public Map<String, Object> initMap(HttpServletRequest request){
		Map<String, Object> map=new HashMap<>();
		String account=request.getParameter("account");
		String shopId=request.getParameter("shopId");
		String postCode=request.getParameter("postCode");
		String status=request.getParameter("status");
		String username=request.getParameter("username");
		String workNumber=request.getParameter("workNumber");
		String mobile=request.getParameter("mobile");
		String idno=request.getParameter("idno");
		if (account!=null) {
			map.put("account", account);
		}
		if (shopId!=null) {
			map.put("shopId", shopId);
		}
		if (postCode!=null) {
			map.put("postCode", postCode);
		}
		if (status!=null) {
			map.put("status", status);
		}
		if (username!=null) {
			map.put("username", username);
		}
		if (workNumber!=null) {
			map.put("workNumber", workNumber);
		}
		if (mobile!=null) {
			map.put("mobile", mobile);
		}
		if (idno!=null) {
			map.put("idno", idno);
		}
		return map;
	}
	
	/**
	 * 员工信息>>>>>>添加跳转页面
	 * @return
	 */
	@RequestMapping("user_add")
	public String user_add( Model model){
		List<BisShop> shopList = shopService.selectByExample();
		model.addAttribute("shopList", shopList);
		
		List<BisShopPost> postList = postService.selectByExample();
		model.addAttribute("postList", postList);
		return null;
	}
	
	/**
	 * 员工信息>>>>>>添加
	 * @param user
	 * @return
	 */
	@RequestMapping("user_to_add")
	public String user_to_add(BisShopUser user,HttpSession session,HttpServletRequest request,@RequestParam("file")MultipartFile file){
		String password=BaseUtils.encrypt(user.getPassword(), BaseUtils.PASSWORD_KEY);
		user.setPassword(password);
		file(file, request, user);
		userService.insert(user,session);
		return "redirect:/bsu/user";
	}
	
	/**
	 * 员工信息>>>>>>修改
	 * @return
	 */
	@RequestMapping("user_update")
	public String user_update(BisShopUser user,HttpSession session,HttpServletRequest request,@RequestParam("file")MultipartFile file){
		String password=BaseUtils.encrypt(user.getPassword(), BaseUtils.PASSWORD_KEY);
		user.setPassword(password);
		if(file.getOriginalFilename()!=null&&!file.getOriginalFilename().equals("")){
			file(file, request, user);
		}
		userService.updateByPrimaryKey(user,session);
		return "redirect:/bsu/user";
	}
	
	/**
	 * 员工信息>>>>>>删除
	 * @return
	 */
	@RequestMapping("user_del")
	public String user_del(String shopUserId){
		BisShopUser bisShopUser = userService.selectByPrimaryKey(shopUserId);
		bisShopUser.setIsDel("1");
		userService.delete(bisShopUser);
		return "redirect:/bsu/user";
	}
	
	/**
	 * 员工信息>>>>>>修改密码
	 * @return
	 */
	@RequestMapping("user_update_password")
	public String user_update_password(String shopUserId,String password2){
		BisShopUser bisShopUser = userService.selectByPrimaryKey(shopUserId);
		String password=BaseUtils.encrypt(password2, BaseUtils.PASSWORD_KEY);
		bisShopUser.setPassword(password);
		userService.delete(bisShopUser);
		return "redirect:/bsu/user";
	}
	
	@RequestMapping(value="account",produces = "application/json;charset=utf-8")
	@ResponseBody
	public String account(String account){
		return userService.selectByAccount(account);
	}
	
	@RequestMapping(value="workNumber",produces = "application/json;charset=utf-8")
	@ResponseBody
	public String workNumber(String workNumber){
		return userService.selectByWorkNumber(workNumber);
	}
	
	@RequestMapping(value="delete",produces = "application/json;charset=utf-8")
	@ResponseBody
	public String delete(String shopUserId){
		userService.deleteByPrimaryKey(shopUserId);
		BisShopUser bisShopUser=userService.selectByPrimaryKey(shopUserId);
		if (bisShopUser==null) {
			return"true";
		}
		return "false";
	}
	
	/**
	 * 文件上传
	 * @param files
	 * @param request
	 * @param bisNurseProject
	 */
	public void file(MultipartFile file,HttpServletRequest request,BisShopUser bisShopUser){//获取项目中的upload的路径
		@SuppressWarnings("deprecation")
		String path=request.getRealPath("/upload");//获取项目的路径
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
		path=path+"/"+sdf.format(new Date());
		File dir=new File(path);
		if(!dir.exists()){//如果当前目录不存在
			dir.mkdirs();//创建一个空的目录
		}
		//获取上传文件名称
		String filename=file.getOriginalFilename();
		//第一个参数表示第二个参数所在的目录
		File newfile=new File(dir,filename);
		if(!newfile.exists()){
			try{
				newfile.createNewFile();//创建一个空的文件
				//将上传的文件内容拷贝到新创建的文件中
				file.transferTo(newfile);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		bisShopUser.setPhoto("/upload/"+sdf.format(new Date())+"/"+file.getOriginalFilename());}
}
